package com.biz.primus.ms.oms.dao.redis;

import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.ms.oms.dao.ro.GenerateTokenRo;
import org.springframework.stereotype.Repository;

/**
 * @author  LGJ
 * @date 2017/11/9
 */
@Repository
public class GenerateTokenRedisDao extends CrudRedisDao<GenerateTokenRo, Long> {

    @Override
    public void save(GenerateTokenRo ro) {
        super.save(ro);
        set(token2IdMappingKey(ro.getGenerateToken()), RedisUtil.toByteArray(ro.getId()));
        set(orderCode2TokenMappingKey(ro.getOrderCode()),RedisUtil.toByteArray(ro.getGenerateToken()));
    }


    public GenerateTokenRo findByToken(String token){
        byte[] idBytes = get(token2IdMappingKey(token));
        Long id = RedisUtil.byteArrayToLong(idBytes);
        return findOne(id);
    }


    public String  findTokenByCode(String orderCode){
        byte[] bytes=get(orderCode2TokenMappingKey(orderCode));
        return RedisUtil.byteArrayToStr(bytes);
    }


    public String token2IdMappingKey(String token) {
        return getKeyByParams("token", token);
    }

    public String orderCode2TokenMappingKey(String orderCode){
        return getKeyByParams("orderCode",orderCode);
    }

}
